﻿Imports System.Web.SessionState
Imports System.Diagnostics

Public Class Global_asax
    Inherits System.Web.HttpApplication

    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the application is started
    End Sub

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the session is started
    End Sub

    'Private sw As Stopwatch = Nothing

    Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires at the beginning of each request
        Dim sw As Stopwatch
        'sw = Stopwatch.StartNew
        ''Session("timer") = sw
        ''Session("total_time") = Nothing
        'Context.Items.Add("timer", sw)
    End Sub

    Private Sub Global_asax_EndRequest(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.EndRequest
        Dim sw As Stopwatch
        'sw = TryCast(Session("timer"), Stopwatch)

        'If sw IsNot Nothing Then
        sw = TryCast(Context.Items("timer"), Stopwatch)

        If sw IsNot Nothing AndAlso sw.IsRunning Then
            sw.Stop()
        End If
        'Response.Write(sw.ElapsedMilliseconds)
        'Session("total_time") = sw.ElapsedMilliseconds
        'End If

    End Sub

    Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires upon attempting to authenticate the use
    End Sub

    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when an error occurs
    End Sub

    Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the session ends
    End Sub

    Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the application ends
    End Sub

End Class